package com.hp.appfw.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class XmlParamUtil {
	public static String packageParam(String data)
	{
		if(data == null)
			return "";
		return "<item>" + data + "</item>"; 
	}
	public static String dataToXml(String xname, Object data) throws Exception
	{
		String str;
		if(xname == null || data == null)
			return "";
		if(data instanceof Date)
	    {
			str = "<" + xname + "><![CDATA[" + TimeUtil.timeFormat((Date)data, "yyyy-MM-dd HH:mm:ss") + "]]></" + xname + ">";
	    }
	    else
	    {
			str = "<" + xname + "><![CDATA[" + data.toString() + "]]></" + xname + ">";
	    }
		return str;
	}
	public static String mapToXml(String xname, Map<String, Object> map)
	{
		if(xname == null || map == null)
			return "";
		StringBuffer info = new StringBuffer();
		
		info.append("<" + xname + ">");
		
		Iterator<Entry<String, Object>> iter = map.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iter.next(); 
		    String key = entry.getKey(); 
		    Object val = entry.getValue();
		    if(val instanceof Date)
		    {
			    info.append("<" + key + "><![CDATA[" + TimeUtil.timeFormat((Date)val, "yyyy-MM-dd HH:mm:ss") + "]]></" + key + ">");
		    }
		    else
		    {
			    info.append("<" + key + "><![CDATA[" + val.toString() + "]]></" + key + ">");
		    }
		} 		

		info.append("</" + xname + ">");
		
		return info.toString();
	}
	public static String classToXml(String xname, Object model)
	{
		if(xname == null || model == null)
			return "";

		StringBuffer info = new StringBuffer();
		
		try{
			info.append("<" + xname + ">");
			Field[] field = model.getClass().getDeclaredFields();
			
			for (int j = 0; j < field.length; j++) { 
				String name = field[j].getName(); 
				String aname = name;
				name = name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toUpperCase()) ;
				String type = field[j].getGenericType().toString(); 
				if (type.equals("class java.lang.String")) { 
					Method m = model.getClass().getMethod("get" + name);
					String value = (String) m.invoke(model); 
					if (value != null) {
						info.append("<" + aname + "><![CDATA[" + value + "]]></" + aname + ">");
					}
					else
					{
						info.append("<" + aname + "></" + aname + ">");
					}
				}
				if (type.equals("class java.lang.Integer")) {
					Method m = model.getClass().getMethod("get" + name);
					Integer value = (Integer) m.invoke(model);
					if (value != null) {
						info.append("<" + aname + "><![CDATA[" + value + "]]></" + aname + ">");
					}
					else
					{
						info.append("<" + aname + "></" + aname + ">");
					}
				}
				if (type.equals("class java.lang.Short")) {
					Method m = model.getClass().getMethod("get" + name);
					Short value = (Short) m.invoke(model);
					if (value != null) {
						info.append("<" + aname + "><![CDATA[" + value + "]]></" + aname + ">");
					}
					else
					{
						info.append("<" + aname + "></" + aname + ">");
					}
				}
				if (type.equals("class java.lang.Double")) {
					Method m = model.getClass().getMethod("get" + name);
					Double value = (Double) m.invoke(model);
					if (value != null) {
						info.append("<" + aname + "><![CDATA[" + value + "]]></" + aname + ">");
					}
					else
					{
						info.append("<" + aname + "></" + aname + ">");
					}
				}
				if (type.equals("class java.lang.Boolean")) {
					Method m = model.getClass().getMethod("get" + name);
					Boolean value = (Boolean) m.invoke(model);
					if (value != null) {
						info.append("<" + aname + "><![CDATA[" + value + "]]></" + aname + ">");
					}
					else
					{
						info.append("<" + aname + "></" + aname + ">");
					}
				}
				if (type.equals("class java.util.Date")) {
					Method m = model.getClass().getMethod("get" + name);
					Date value = (Date) m.invoke(model);
					if (value != null) {
						info.append("<" + aname + "><![CDATA[" + TimeUtil.timeFormat(value, "yyyy-MM-dd HH:mm:ss") + "]]></" + aname + ">");
					}
					else
					{
						info.append("<" + aname + "></" + aname + ">");
					}
				}
			}
			info.append("</" + xname + ">");
		}
		catch(Exception e)
		{
			info.append(e.toString() + e.getMessage());
		}
		return info.toString();
	}

}
